#include "StdAfx.h"
#include "terminalwidget.h"
#include "configdialog.h"

TerminalWidget::TerminalWidget(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	serialPort = new QextSerialPort(QextSerialPort::EventDriven);
	
	connect(ui.connectButton, SIGNAL(clicked()), this, SLOT(slotConnectPort()));
	connect(ui.deconnectButton, SIGNAL(clicked()), this, SLOT(slotDisconnectPort()));
	connect(ui.sendCommandButton, SIGNAL(clicked()), this, SLOT(slotSendCommand()));
	connect(ui.configurationButton, SIGNAL(clicked()), this, SLOT(slotOpenConfigDialog()));

	connect(serialPort, SIGNAL(readyRead()), this, SLOT(slotOnReadyRead()));

	QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
	for (unsigned int i = 0; i < ports.size(); i++) 
	{
		ui.comboBoxPortName->addItem(ports[i].portName);
	}


	
	ui.deconnectButton->setEnabled(false);
	ui.sendCommandButton->setEnabled(false);
}

TerminalWidget::~TerminalWidget()
{

}

void TerminalWidget::slotConnectPort()
{
	qDebug() << "slotConnectPort()";
	
	loadSettings();
	if (serialPort->isOpen())
        serialPort->close();

	if(serialPort->open(QIODevice::ReadWrite))
	{
		qDebug() << "Connexion reussi";
		ui.deconnectButton->setEnabled(true);
		ui.sendCommandButton->setEnabled(true);
	}
	else
	{
		qDebug() << "Pas de Connexion";
	}

}

void TerminalWidget::slotDisconnectPort()
{
	qDebug() << "slotDisconnectPort()";
	serialPort->close();
	ui.deconnectButton->setEnabled(false);
	ui.sendCommandButton->setEnabled(false);
}

void TerminalWidget::slotSendCommand()
{
	qDebug() << "slotSendCommand()";
	QString Message;
    Message = ui.commandEdit->text();
	
	QByteArray temp(Message.toUtf8());
    serialPort->write(temp);
	ui.commandEdit->clear();

}

void TerminalWidget::slotOpenConfigDialog()
{
	qDebug() << "slotOpenConfigDialog()";
	dialog = new ConfigDialog(this);
    dialog->exec();
}

void TerminalWidget::slotOnCloseConfigDialog()
{
	qDebug() << "slotCloseConfigDialog()";
	loadSettings();
}

void TerminalWidget::loadSettings()
{
	dialog = new ConfigDialog(this);
	dialog->init();
	PortSettings ps = dialog->getPortSettings();
	serialPort->setPortName(ui.comboBoxPortName->itemText(ui.comboBoxPortName->currentIndex()));
	serialPort->setBaudRate(ps.BaudRate);
	serialPort->setDataBits(ps.DataBits);
	serialPort->setFlowControl(ps.FlowControl);
	serialPort->setParity(ps.Parity);
	serialPort->setStopBits(ps.StopBits);
	serialPort->setTimeout(ps.Timeout_Millisec);
	codec = dialog->getCodecs();

}

void TerminalWidget::slotOnReadyRead()
{
	QByteArray dataReceived;
    int a = serialPort->bytesAvailable();
    dataReceived.resize(a);
    serialPort->read(dataReceived.data(), dataReceived.size());

	ui.resultText->moveCursor(QTextCursor::End,  QTextCursor::MoveAnchor);

    QTextStream in(dataReceived);
    in.setAutoDetectUnicode(false);
    in.setCodec(codec);
    QString decodedStr = in.readAll();

    ui.resultText->insertPlainText(decodedStr);
}